﻿using System;
using System.Web.Script.Services;
using System.Web.Services;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace DE.Sdc.SharePoint.Common.Deployment.Administration
{
    /// <summary>
    /// Offers the functionality to execute the methods in context of the central administration
    /// </summary>
    [WebService(Namespace = "http://sdc/2009/12/WebServices/1.0/AdminTasksWebService")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class AdminTasksWebService : WebService
    {
        public const string AdminTaskWebServiceUrl = "../_vti_bin/AdminTasksWebService.asmx";

        /// <summary>
        /// Creates a timer job
        /// </summary>
        /// <param name="jobSettings">Settings for the job that should created</param>
        [WebMethod]
        public void CreateJob(JobSettings jobSettings)
        {
            using (SPSite site = new SPSite(jobSettings.SiteUrl))
            {
                object[] parameters = { jobSettings.JobName, site.WebApplication };
                SPJobDefinition jobDefinition = Activator.CreateInstance(Type.GetType(jobSettings.JobType), parameters) as SPJobDefinition;

                if (jobDefinition != null)
                {

                    Period schedulingPeriod = (Period)Enum.Parse(typeof(Period), jobSettings.SchedulingInformation["AutomaticSchedulingDuration"].Value);
                    jobDefinition.Schedule = AutomaticScheduling.CreateJobSchedule(schedulingPeriod,
                                                                                   int.Parse(jobSettings.SchedulingInformation["AutomaticSchedulingStart"].Value),
                                                                                   int.Parse(jobSettings.SchedulingInformation["AutomaticSchedulingEnd"].Value));

                    foreach (var parameter in jobSettings.Parameters)
                    {
                        jobDefinition.Properties.Add(parameter.Name, parameter.Value);
                    }
                    
                    jobDefinition.Update();
                }
            }
        }

        /// <summary>
        /// Deletes a timer job
        /// </summary>
        /// <param name="jobName">Name of the job</param>
        /// <param name="siteId">Id of the site of a webapplication where the job belongs to</param>
        [WebMethod]
        public void DeleteJob(string jobName, string siteId)
        {
            using (SPSite site = new SPSite(new Guid(siteId)))
            {
                // delete the job
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (job.Name == jobName)
                        job.Delete();
                }
            }
        }

        /// <summary>
        /// Indicates whether a timer job exists or not
        /// </summary>
        /// <param name="jobName">Name of the job</param>
        /// <param name="siteId">Id of the site of a webapplication where the job belongs to</param>
        /// <returns></returns>
        [WebMethod]
        public bool IsJobDefined(string jobName, string siteId)
        {
            using (SPSite site = new SPSite(new Guid(siteId)))
            {
                SPWebApplication webApp = site.WebApplication;

                foreach (SPJobDefinition job in webApp.JobDefinitions)
                {
                    if (job.Name.Equals(jobName))
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Modifies the web.config 
        /// </summary>
        /// <param name="configurationPath">Absolute path to the configuration file</param>
        /// <param name="url">Url of the site that web.config should updated</param>
        /// <param name="removeModifications">Indicates whether the modifications of the web.config should be added to or removed from it</param>
        [WebMethod]
        public void ModifyWebConfig(string configurationPath, string url, bool removeModifications)
        {
            using (SPSite site = new SPSite(url))
            {
                string webUrl = url.Remove(0, site.Url.Length);

                using (SPWeb web = site.OpenWeb(webUrl))
                {
                    AppConfigModifier.Execute(configurationPath, web, false, true);
                }
            }

        }
    }
}
